/* 系统信息和系统资源 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/utsname.h>
#include <sys/sysinfo.h>

int main(int argc,char **argv)
{
    clock_t t_start;
    clock_t t_end;
    struct utsname info;
    struct sysinfo sys_info;
    struct tm locatl_t;
    time_t sec;
    int i=0;
    char tm_str[100] = {0};
    char buff[50] = {0};

    //获取开始时间
    t_start = clock();
    if(t_start == -1) {
        perror("clock error");
        exit(-1);
    }

    /* 验证main函数传参 */
    printf("count: %d\n",argc);
    for(i=0;i<argc;i++) {
        printf("%s\n",argv[i]);
    }
    //获取系统标识
    if(-1 == uname(&info)) {
        perror("uname error");
        exit(-1);
    }
    //获取系统统计信息
    if(-1 == sysinfo(&sys_info)) {
        perror("sysinfo error");
        exit(-1);
    }
    //获取主机name
    if(-1 == gethostname(buff,sizeof(buff))) {
        perror("gethostname error");
        exit(-1);
    } 
    printf("hostname: %s\n",buff);

    /* 打印信息 */ 
    printf("操作系统名称: %s\n", info.sysname); 
    printf("主机名: %s\n", info.nodename); 
    printf("内核版本: %s\n", info.release); 
    printf("发行版本: %s\n", info.version); 
    printf("硬件架构: %s\n", info.machine);

    printf("uptime: %ld\n", sys_info.uptime); 
    printf("totalram: %lu\n", sys_info.totalram); 
    printf("freeram: %lu\n", sys_info.freeram); 
    printf("procs: %u\n", sys_info.procs); 

    //获取时间
    sec = time(NULL);
    if(-1 == sec) {
        perror("timr error");
        exit(-1);
    }
    localtime_r(&sec,&locatl_t);
    strftime(tm_str,sizeof(tm_str),"%Y-%m-%d %A %H:%M:%S",&locatl_t);
    printf("当前时间:%s\n",tm_str);

    //获取结束时间
    t_end = clock();
    if(t_end == -1) {
        perror("clock error");
        exit(-1);
    }

    printf("进程总CPU时间: %f\n",(t_end-t_start)/(double)CLOCKS_PER_SEC);
 
    return 0;
}